package com.kingscup.gamestates;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import com.kingscup.Storage;

public class InGame extends BasicGameState {

        private int stateID;
        boolean showCardtext;
        // Tekee olion PlayerMenusta että voi hakea muuttujia, niitten pitää olla
        // staattisia että voi viitata toisessa
        // Javassatiedostossa niihin
        Input input;
        int randomPosition;
        int arrayNumber = 23;
        int saanto1 = 0;
        int saanto2 = 4;
        int saanto3 = 8;
        int saanto4 = 12;
        int saanto5 = 16;
        int saanto6 = 20;
        
        int a = 0;
        
        int korttipakka = 24;
        int pressed = 0;
        
        
        PlayerMenu playerMenu = new PlayerMenu(stateID);
        Random rand = new Random();
        String pelisana;

        Image player1;
        Image player2;
        Image player3;
        Image player4;
        Image player5;
        Image player6;
        
        Image play;
        MouseOverArea playy;
        int[] randomValues;
        Image background;
        
        

        public InGame(int stateID) {
                this.stateID = stateID;
        }
        
        
        
        @Override
        public void init(GameContainer gameContainer, StateBasedGame sbg)
                        throws SlickException {
        	
        	// Haetaan ArrayList	
        	Storage.numbers = new ArrayList<Integer>();
        		
        	// Tungetaan listaan numerot 0 - 23
        		for(int i = 0; i < 24; i++)
        			{
        			Storage.numbers.add(i);
        				}
 		
        	//	Sekotetaan ArrayListin numerot
        		Collections.shuffle(Storage.numbers);		
        		System.out.println(Storage.numbers); 
 		
			 	
			 	       	
                background = new Image("assets/menu.jpg");
                player1 = new Image("assets/player1.png");
                player2 = new Image("assets/player1.png");
                player3 = new Image("assets/player1.png");
                player4 = new Image("assets/player1.png");
                player5 = new Image("assets/player1.png");
                player6 = new Image("assets/player1.png");
                
                play = new Image("assets/play.png");
                playy = new MouseOverArea(gameContainer, play, 240, 500);
                
        }

        @Override
        public void render(GameContainer gameContainer, StateBasedGame sbg,
                        Graphics g) throws SlickException {
                background.draw(0, 0, 1024, 768);
                playy.render(gameContainer, g);
                
                if (Storage.players == 2) {
                        player1.draw(100, 660, 100, 100);
                        player2.draw(250, 660, 100, 100);
                }
                if (Storage.players == 3) {
                        player1.draw(100, 660, 100, 100);
                        player2.draw(250, 660, 100, 100);
                        player3.draw(400, 660, 100, 100);
                }
                if (Storage.players == 4) {
                        player1.draw(100, 660, 100, 100);
                        player2.draw(250, 660, 100, 100);
                        player3.draw(400, 660, 100, 100);
                        player4.draw(550, 660, 100, 100);
                }
                if (Storage.players == 5) {
                        player1.draw(100, 660, 100, 100);
                        player2.draw(250, 660, 100, 100);
                        player3.draw(400, 660, 100, 100);
                        player4.draw(550, 660, 100, 100);
                        player5.draw(700, 660, 100, 100);
                }
                if (Storage.players == 6) {
                        player1.draw(100, 660, 100, 100);
                        player2.draw(250, 660, 100, 100);
                        player3.draw(400, 660, 100, 100);
                        player4.draw(550, 660, 100, 100);
                        player5.draw(700, 660, 100, 100);
                        player6.draw(850, 660, 100, 100);
                }

                if (korttipakka == 0) { 
                	g.drawString("PELI LOPPUI", gameContainer.getWidth()/2, gameContainer.getHeight()/2);
                	showCardtext = true;
                }
                       
                if(!showCardtext) {
                g.drawString(" " + pelisana, 100, 100);
                }
                
                g.drawString("Kortteja on jäljellä: " + Integer.toString(korttipakka), 780, 20);
                
        }

        @Override
        public void update(GameContainer gameContainer, StateBasedGame sbg, int delt)
                        throws SlickException {
        	
        	
        	input = gameContainer.getInput();
            input.clearMousePressedRecord();
            
            if (korttipakka > 0) {
            if ( playy.isMouseOver() && input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)
                    ){
      			 
   			 	pressed++;
   			 	
   			 	if(pressed == 1){
   			 		
   			 			input.clearMousePressedRecord();
   			 		   			 			
        				pelisana = Storage.saantoTaulu[Storage.numbers.get(arrayNumber)]; 
        				        			       				        				
        				if(pelisana.equals("Juo 1")) { 
        					Storage.numbers.remove(arrayNumber);
        					arrayNumber -= 1;       					
        					korttipakka -= 1;       					
        					System.out.println(Storage.numbers);        				
        				}
        				if(pelisana.equals("Juo 2")) { 
        					Storage.numbers.remove(arrayNumber);
        					arrayNumber -= 1;       					
        					korttipakka -= 1;       					
        					System.out.println(Storage.numbers);
        				} 
        				if(pelisana.equals("Juo 3")) { 
        					Storage.numbers.remove(arrayNumber);
        					arrayNumber -= 1;       					
        					korttipakka -= 1;       					
        					System.out.println(Storage.numbers);
        				}
        				if(pelisana.equals("Määrää 1")) { 
        					Storage.numbers.remove(arrayNumber);
        					arrayNumber -= 1;       					
        					korttipakka -= 1;       					
        					System.out.println(Storage.numbers);
        				} 
        				if(pelisana.equals("Määrää 2")) { 
        					Storage.numbers.remove(arrayNumber);
        					arrayNumber -= 1;       					
        					korttipakka -= 1;       					
        					System.out.println(Storage.numbers);
        				} 
        				if(pelisana.equals("Määrää 3")) { 
        					Storage.numbers.remove(arrayNumber);
        					arrayNumber -= 1;       					
        					korttipakka -= 1;       					
        					System.out.println(Storage.numbers);
        				} 
        				
   			 	
   			 	}
        				
        				
        				/*System.out.println("SÄÄNTÖJÄ 1 MENNYT " + saanto1);
        				System.out.println("SÄÄNTÖJÄ 2 MENNYT " + (saanto2-4));
        				System.out.println();
        				System.out.println("Pakassa on " + korttipakka + " korttia");*/
   			 			
            } }
   			 		 	
   			 	
        if(playy.isMouseOver() && !input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) { 
	   			
				pressed = 0;
				
				
			
		}
        
        }

        @Override
        public int getID() {
                // TODO Auto-generated method stub
                return stateID;
        }

}